package org.tgcloud.group.core.date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tgcloud.group.core.BeanUtil;
import org.tgcloud.group.core.DateUtil;
import org.tgcloud.group.core.RedisUtil;
import org.tgcloud.group.entity.SysDate;
import org.tgcloud.group.repository.SysDateMapper;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: zhy
 * @Created: 2024-07-08-上午11:15
 * @Description:
 */
@Component
public class DateUtils {

    @Autowired
    private SysDateMapper sysDateMapper;

    private static String holidayKey = "holiday:";

    public List<SysDate> getDateList(Long startTime, Long endTime) {
        List<SysDate> dateList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd");
        List<Date> dateListByStartTimeAndEndTime = DateUtil.getDateListByStartTimeAndEndTime(new Date(startTime), new Date(endTime));
        for (Date date : dateListByStartTimeAndEndTime) {
            String format = sdf.format(date);
            if (!RedisUtil.existKey(holidayKey + format)) {
                SysDate sysDate = sysDateMapper.selectOneDayByDate(date);
                RedisUtil.setCacheObject(holidayKey + format, sysDate);
                dateList.add(sysDate);
            } else {
                SysDate sysDate = RedisUtil.getCacheObject(holidayKey + format);
                dateList.add(sysDate);
            }
        }
        return dateList;
    }

    public Long getHolidayNumByTime(Long startTime, Long endTime){
        List<SysDate> dateList = getDateList(startTime, endTime);
        List<SysDate> finalDateList = new ArrayList<>();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        String startString = sdf1.format(startTime);
        String endString = sdf1.format(endTime);
        for (SysDate sysDate : dateList){
            if (sysDate != null){
                String format = sdf1.format(sysDate.getDate());
                if (Integer.valueOf(format) >= Integer.valueOf(startString) && Integer.valueOf(format) <= Integer.valueOf(endString)){
                    finalDateList.add(sysDate);
                }
            }
        }
        return getHolidayNumByTimeListAndStartAndEnd(finalDateList, startTime, endTime);
    }


    // dateList是开始时间到结束时间里的所有时间
    public static Long getHolidayNumByTimeListAndStartAndEnd(List<SysDate> dateList, Long startTime, Long endTime){
        Integer startTimeType = 0; // 1、开始时间不是工作日
        Integer endTimeType = 0; // 1、结束时间不是工作日
        Integer holidayNum = 0; // 节假日或者休息日数量
        Long diffTime = 0L;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String startString = sdf.format(startTime);
        String endString = sdf.format(endTime);
        if (!BeanUtil.isNullList(dateList)){
            for (SysDate sysDate : dateList){
                String format = sdf.format(sysDate.getDate());
                if (Integer.valueOf(format) >= Integer.valueOf(startString) && Integer.valueOf(format) <= Integer.valueOf(endString)){
                    if (sysDate.getDayType() == 3 || sysDate.getDayType() == 4){
                        // 3、周末休息日，4、节假日
                        holidayNum = holidayNum + 1;
                        // 判断开始时间是否是工作日
                        if (format.equals(startString)){
                            startTimeType = 1;
                        }
                        // 判断结束时间是否是工作日
                        if (format.equals(endString)){
                            endTimeType = 1;
                        }
                    }
                }
            }
            if (startTimeType == 1){
                holidayNum = holidayNum - 1;
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date(startTime)); // 把当前时间赋给日历
                calendar.add(calendar.DATE, 1); // 设置为后一天
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                Date time = calendar.getTime();
                startTime = time.getTime();
            }
            if (endTimeType == 1){
                holidayNum = holidayNum - 1;
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date(endTime)); // 把当前时间赋给日历
                calendar.add(calendar.DATE, -1); // 设置为前一天
                calendar.set(Calendar.HOUR_OF_DAY, 23);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);
                calendar.set(Calendar.MILLISECOND, 999);
                Date time = calendar.getTime();
                endTime = time.getTime();
            }
            if (holidayNum > 0){
                diffTime = holidayNum * 24 * 60 * 60L;
            }
        }
        Long timeDiff = endTime / 1000 - startTime / 1000 - diffTime;
        return timeDiff;
    }

    // 获取所有的天的数据
    public static List<SysDate> getAllDateList(){
        Set<String> keys = RedisUtil.keys("holiday:*");
        List<SysDate> sysDateList = new ArrayList<>();
        for (String key : keys){
            SysDate sysDate = RedisUtil.getCacheObject(key);
            sysDateList.add(sysDate);
        }
        return sysDateList;
    }

    // 获取所有节假日以及休息日的数据
    public static List<SysDate> getAllHolidayDateList(){
        Set<String> keys = RedisUtil.keys("holiday:*");
        List<SysDate> sysDateList = new ArrayList<>();
        for (String key : keys){
            SysDate sysDate = RedisUtil.getCacheObject(key);
            if (sysDate.getDayType() == 3 || sysDate.getDayType() == 4){
                sysDateList.add(sysDate);
            }
        }
        return sysDateList;
    }

}
